
<#include "common/list-header.ftl">
<@navbar home="首页" homeUrl="${ctx.contextPath}/bootstrap/css/bootstrap.css"/>
<!-- DIV 弹窗控件-->
	<div  id="createItem" class="modal fade" role="dialog" aria-labelledby="gridSystemModalLabel">
	  <div class="modal-dialog" role="document">
		    <div class="modal-content">
			      <div class="modal-header">
			        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			        <h4 class="modal-title" id="gridSystemModalLabel">创建</h4>
			      </div>
			      <div class="modal-body">
				        <div class="container-fluid">
				           <form class="form-horizontal col-md-12">
							  <div class="form-group">
							    <label for="name"  class="col-sm-2 control-label" >名称</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text"  id="name" name="name"  class="form-control "placeholder="插件名称">
								</div>
							  </div>
							  <div class="form-group">
							    <label for="descript"  class="col-sm-2 control-label" >介绍</label>
								<div class='col-sm-8 '>	   
								    <textarea   id="descript" name="descript"  class="form-control" rows="2"></textarea> 
								</div>
							  </div>
							  <div class="divider" role="separator"></div>
							  <div class="form-group">
							    <label for="driver"  class="col-sm-2 control-label" >驱动</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text" value="com.mysql.jdbc.Driver" id="driver" name="driver"  class="form-control "placeholder="jdbc 驱动">
								</div>
							  </div>
							  <div class="form-group">
							    <label for="url"  class="col-sm-2 control-label" >url</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text"  id="url" name="url"  class="form-control "placeholder="jdbc url">
								</div>
							  </div>
							  <div class="form-group">
							    <label for="username"  class="col-sm-2 control-label" >username</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text"  id="username" name="username"  class="form-control "placeholder="db username">
								</div>
							  </div>
							  <div class="form-group">
							    <label for="password"  class="col-sm-2 control-label" >password</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text"  id="password" name="password"  class="form-control "placeholder="db password">
								</div>
							  </div>
							  <div class="form-group">
							    <label for="sqlMap"  class="col-sm-2 control-label" >sql-map-file</label>
								<div class='col-sm-8 '>	    
							   		 <input type="text"  id="sqlMap" name="sqlMap" disabled value="classpath:ibatis/sql-map-mysql-sms-config.xml" class="form-control "placeholder="mybatis sql mapping file">
								</div>
							  </div> 
							</form>
				        </div>
			      </div>
			      <div class="modal-footer">
				      <div class="btn-toolbar col-md-4" role="toolbar" style="left:35%;" aria-label="...">
				      	<div class="btn-group" role="group" aria-label="...">
				       		 <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						</div>
				      	<div class="btn-group" role="group" aria-label="...">
				      	 	 <button type="button" onclick="saveData('${ctx.contextPath}/rest/smsDatabasePlugin/save','createItem')" class="btn btn-primary">保存</button>
						</div>
					</div>
			      </div>
		    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->
<div  id="modifyItem" class="modal fade" role="dialog" aria-labelledby="gridSystemModalLabel">

</div><!-- /.modal -->	
<!-- DIV 弹窗控件结束-->
	<div class="panel panel-success col-md-12">
			<div class="panel-body">
				
			</div>
			<table  class="table col-md-12 table-striped table-bordered table-hover ">
					<caption>
						<div class="btn-group pull-right" role="group" aria-label="...">
						  <button type="button"  data-target="#createItem" data-toggle="modal" class="btn btn-default btn-xs">新增</button>
						</div>
					</caption>
					<thead>
						<tr> 
							<th class="col-md-1">插件名称</th>
							<th class="col-md-2">插件描述</th>
							<!-- <th class="col-md-2">DB驱动</th>
							<th class="col-md-4">DB地址</th>-->
							<th class="col-md-1">DB用户名</th>
							<th >插件状态</th>
							<th >短信状态</th>
							<th>操作
							</th>
						</tr>
					</thead>
					<tbody>
					<#list pages.data as data>
						<tr>
							<td>${data.name!""}</td>
							<td>${data.descript!""}</td>
							<td>${data.username!""}</td>
							<td><#if data.status>
									<a class="btn btn-danger btn-xs" onclick="setStatus('${data.name!""}',false)">禁用</a>
								<#else>
									<a class="btn btn-success btn-xs" onclick="setStatus('${data.name!""}',true)">启用</a>
								</#if>
							</td>
							<td>
								<span id="search_${data.name!""}">
								</span>
								<button type="button" data-loading-text="刷新..."   onclick="reFresh('${data.name!""}',this)" class="btn btn-info btn-xs" autocomplete="off">
								  刷新
								</button>
							</td>
							<td>
								<a class="btn btn-danger btn-xs" onclick="deleteItem('${data.name!""}')"> 删除</a>
								<a class="btn btn-info  btn-xs" onclick="modifyItem('${data.name!""}')"> 修改</a>
							</td>
						</tr>
					</#list>
				   	</tbody>
			</table>
	</div>
    <script >
    	function reFresh(id,obj){
		    var $btn = $(obj).button('loading');
		    $.get("${ctx.contextPath}/rest/sms/task/"+id+"/status",function(data){
    			if(data.respCode=="success"){
	    			$('#search_'+id).empty();
	    			var text='';
    				if(data.respList[0].status==1){
						text+='<span  style="margin-right: 2px;" class="label label-success">正常</span>';
						text+='<button class="btn btn-danger btn-xs" data-loading-text="停止中..."   onclick="stop(\''+id+'\',this)"  autocomplete="off">停止</button>';
    				}else{
						text+='<span style="margin-right: 2px;" data-content="'+data.respList[0].errorMsg+'" class="label label-danger" data-trigger="focus" title="异常详情" tabindex="0" data-placement="top" role="button" data-toggle="popover">异常</span>';
						text+='<a class="btn btn-success btn-xs" data-loading-text="启动中..."   onclick="start(\''+id+'\',this)"  autocomplete="off">启动</a>';
    				}
	    			$('#search_'+id).append(text);
    			}else if(data.respCode=="warning"){//启动按钮
	    			$('#search_'+id).empty();
	    			var text='';
					text+='<span class="label label-danger"  style="margin-right: 2px;" >未启动</span>';
					text+='<a class="btn btn-success btn-xs" data-loading-text="启动中..."   onclick="start(\''+id+'\',this)"  autocomplete="off">启动</a>';
	    			$('#search_'+id).append(text);
    			}else if(data.respCode=="error"){
    				alert(data.respMessage);
    			}else{
    				alert("系统忙");
    			}
    			$btn.button('reset');
    			$('[data-toggle="popover"]').popover();//初始化弹窗
    		})
    	}
    	function stop(id,obj){
		    setTaskStatus(id,obj,"false");
    	}
    	function start(id,obj){
		    setTaskStatus(id,obj,"true");
    	}
    	function setTaskStatus(id,obj,status){
		    var $btn = $(obj).button('loading');
		    $.post("${ctx.contextPath}/rest/sms/task/set/"+id+"/"+status,function(data){
    			if(data.respCode=="success"){
	    			window.setTimeout(function(){
							reFresh(id,obj);
			    			$btn.button('reset');
						},3000); 
    			}else if(data.respMessage){
    				alert(data.respMessage);
	    			$btn.button('reset');
    			}else{
    				alert("系统忙");
	    			$btn.button('reset');
    			}
    		})
    	}
    	function modifyItem(id){
    		$.get("${ctx.contextPath}/rest/smsDatabasePlugin/details/"+id,function(data){
    			$('#modifyItem').empty();
    			$('#modifyItem').append(data);
    			$('#modifyItem').modal("show");
    		})
    	}
    	function deleteItem(id){
    		$.get("${ctx.contextPath}/rest/smsDatabasePlugin/delete/"+id,function(data){
    			if(data.respCode=="success"){
    				window.location.reload();
    			}else if(data.respMessage){
    				alert(data.respMessage);
    			}else{
    				alert("系统忙");
    			}
    		})
    	}
    	function setStatus(id,status){
    		$.post("${ctx.contextPath}/rest/smsDatabasePlugin/set/"+id+"/"+status,function(data){
    			if(data.respCode=="success"){
    				window.location.reload();
    			}else if(data.respMessage){
    				alert(data.respMessage);
    			}else{
    				alert("系统忙");
    			}
    		})
    	}
    </script>
<#include "common/list-footer.ftl">